<script setup lang="ts">
import { useAdStore } from '@/stores/adStore'
import AnimatedChart from '@/components/AnimatedChart.vue'
import ParticleBackground from '@/components/ParticleBackground.vue'
import { ref } from 'vue'

const adStore = useAdStore()
const timeRange = ref('realtime')
const isDev = import.meta.env.DEV

const handleFilterChange = () => {
  adStore.stopAutoRefresh()
  adStore.fetchData(timeRange.value)
  adStore.startAutoRefresh()
}
</script>

<template>
  <div class="dashboard">
    <h1>广告数据看板</h1>

    <!-- 错误信息显示 -->
    <div v-if="adStore.error" class="error-message">
      <p>⚠️ {{ adStore.error }}</p>
      <button @click="adStore.fetchData(timeRange)" class="retry-btn">重试</button>
    </div>

    <div class="filters">
      <select v-model="timeRange" @change="handleFilterChange">
        <option value="realtime">实时数据</option>
        <option value="hourly">小时数据</option>
        <option value="daily">日数据</option>
      </select>
    </div>

    <ParticleBackground />

    <div class="charts">
      <AnimatedChart title="广告曝光量" :data="adStore.getCurrentExposureData(timeRange)" :loading="adStore.loading" />
      <AnimatedChart title="点击量" :data="adStore.getCurrentClickData(timeRange)" :loading="adStore.loading" />
    </div>

    <!-- 调试信息 -->
    <div class="debug-info" v-if="isDev">
      <details>
        <summary>调试信息</summary>
        <p>加载状态: {{ adStore.loading }}</p>
        <p>错误信息: {{ adStore.error || '无' }}</p>
        <p>曝光数据长度: {{ adStore.getCurrentExposureData(timeRange).length }}</p>
        <p>点击数据长度: {{ adStore.getCurrentClickData(timeRange).length }}</p>
        <p>当前时间范围: {{ timeRange }}</p>
      </details>
    </div>
  </div>
</template>

<style scoped>
.dashboard {
  padding: 25px;
  max-width: 2000px;
  margin: 10px auto;
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
  background: linear-gradient(135deg,
      #667eea 0%,
      #764ba2 25%,
      #f093fb 50%,
      #f5576c 75%,
      #4facfe 100%);
  background-size: 400% 400%;
  min-height: calc(100vh - 20px);
  position: relative;
  animation: gradientShift 15s ease infinite;
  border-radius: 25px;
  box-shadow:
    0 15px 30px -8px rgba(0, 0, 0, 0.2),
    0 8px 16px -4px rgba(0, 0, 0, 0.1);
  display: flex;
  flex-direction: column;
}

.dashboard::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background:
    radial-gradient(circle at 20% 50%, rgba(120, 119, 198, 0.3) 0%, transparent 50%),
    radial-gradient(circle at 80% 20%, rgba(255, 119, 198, 0.3) 0%, transparent 50%),
    radial-gradient(circle at 40% 80%, rgba(120, 200, 255, 0.3) 0%, transparent 50%);
  pointer-events: none;
  animation: floatingBubbles 20s ease-in-out infinite;
}

h1 {
  color: #ffffff;
  text-align: center;
  margin-bottom: 18px;
  font-size: 2.2rem;
  font-weight: 700;
  letter-spacing: -1px;
  position: relative;
  padding-bottom: 12px;
  text-shadow: 0 4px 8px rgba(0, 0, 0, 0.3);
  animation: titleGlow 3s ease-in-out infinite alternate;
}

h1::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 120px;
  height: 4px;
  background: linear-gradient(90deg,
      rgba(255, 255, 255, 0.8),
      rgba(255, 255, 255, 0.4),
      rgba(255, 255, 255, 0.8));
  border-radius: 2px;
  animation: underlineGlow 2s ease-in-out infinite;
}

.error-message {
  background: rgba(254, 226, 226, 0.95);
  border: 1px solid rgba(254, 202, 202, 0.8);
  border-radius: 12px;
  padding: 20px;
  margin-bottom: 24px;
  text-align: center;
  color: #dc2626;
  backdrop-filter: blur(10px);
  box-shadow: 0 8px 32px rgba(220, 38, 38, 0.1);
  animation: slideInDown 0.5s ease-out;
}

.retry-btn {
  background: linear-gradient(135deg, #dc2626, #ef4444);
  color: white;
  border: none;
  padding: 10px 20px;
  border-radius: 8px;
  margin-left: 12px;
  cursor: pointer;
  transition: all 0.3s cubic-bezier(0.4, 0.0, 0.2, 1);
  font-weight: 500;
  box-shadow: 0 4px 6px rgba(220, 38, 38, 0.3);
}

.retry-btn:hover {
  background: linear-gradient(135deg, #b91c1c, #dc2626);
  transform: translateY(-2px);
  box-shadow: 0 8px 15px rgba(220, 38, 38, 0.4);
}

.filters {
  display: flex;
  justify-content: center;
  margin-bottom: 20px;
  animation: fadeInUp 0.8s ease-out 0.3s both;
}

select {
  padding: 12px 32px;
  border-radius: 12px;
  border: 1px solid rgba(255, 255, 255, 0.3);
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  font-size: 16px;
  font-weight: 500;
  color: #ffffff;
  box-shadow:
    0 8px 32px rgba(0, 0, 0, 0.1),
    inset 0 1px 0 rgba(255, 255, 255, 0.2);
  transition: all 0.3s cubic-bezier(0.4, 0.0, 0.2, 1);
  cursor: pointer;
  min-width: 200px;
  text-align: center;
}

select:focus {
  outline: none;
  border-color: rgba(255, 255, 255, 0.6);
  background: rgba(255, 255, 255, 0.15);
  box-shadow:
    0 0 0 4px rgba(255, 255, 255, 0.1),
    0 8px 32px rgba(0, 0, 0, 0.2);
  transform: translateY(-2px);
}

select option {
  background: #1e293b;
  color: #ffffff;
  padding: 8px;
}

.charts {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 25px;
  flex: 1;
  animation: fadeInUp 1s ease-out 0.6s both;
}

.debug-info {
  margin-top: 15px;
  padding: 12px;
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  border-radius: 8px;
  font-size: 12px;
  color: rgba(255, 255, 255, 0.9);
  border: 1px solid rgba(255, 255, 255, 0.2);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
  animation: fadeInUp 1.2s ease-out 0.9s both;
}

.debug-info summary {
  cursor: pointer;
  font-weight: 500;
  margin-bottom: 8px;
  color: #ffffff;
  transition: all 0.3s ease;
  font-size: 13px;
}

.debug-info summary:hover {
  color: rgba(255, 255, 255, 0.8);
}

.debug-info p {
  margin: 4px 0;
  opacity: 0.9;
  font-size: 11px;
}

/* 动画定义 */
@keyframes gradientShift {
  0% {
    background-position: 0% 50%;
  }

  50% {
    background-position: 100% 50%;
  }

  100% {
    background-position: 0% 50%;
  }
}

@keyframes floatingBubbles {

  0%,
  100% {
    transform: translateY(0px);
    opacity: 1;
  }

  33% {
    transform: translateY(-30px);
    opacity: 0.8;
  }

  66% {
    transform: translateY(-15px);
    opacity: 0.9;
  }
}

@keyframes titleGlow {
  0% {
    text-shadow: 0 4px 8px rgba(0, 0, 0, 0.3);
  }

  100% {
    text-shadow:
      0 4px 8px rgba(0, 0, 0, 0.3),
      0 0 20px rgba(255, 255, 255, 0.3);
  }
}

@keyframes underlineGlow {

  0%,
  100% {
    opacity: 0.8;
    transform: translateX(-50%) scaleX(1);
  }

  50% {
    opacity: 1;
    transform: translateX(-50%) scaleX(1.1);
  }
}

@keyframes slideInDown {
  from {
    opacity: 0;
    transform: translateY(-30px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .dashboard {
    padding: 20px 15px;
    margin: 5px;
    border-radius: 20px;
    height: calc(100vh - 10px);
  }

  h1 {
    font-size: 1.8rem;
    margin-bottom: 15px;
  }

  .charts {
    grid-template-columns: 1fr;
    gap: 15px;
  }

  .filters {
    margin-bottom: 20px;
  }

  select {
    min-width: 160px;
    padding: 8px 20px;
  }
}

@media (min-width: 1200px) {
  .charts {
    grid-template-columns: repeat(2, 1fr);
    gap: 35px;
  }

  .dashboard {
    padding: 40px;
    max-width: 2200px;
    border-radius: 30px;
  }
}

/* 平滑滚动 */
@media (prefers-reduced-motion: no-preference) {
  html {
    scroll-behavior: smooth;
  }
}
</style>
